package api

import (
	"Wechat/Tools"
	"Wechat/domain"
	"Wechat/service"
	"fmt"
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
)

// 使用ForumService来调用service端函数
var forumservice service.ForumService

// AddForumText 添加帖子
func AddForumText(c *gin.Context) {
	var forum domain.Forum
	//读取页面数据填写
	var forumMap = make(map[string]interface{})
	c.BindJSON(&forumMap)
	fmt.Println(forumMap)
	forum.Title = forumMap["title"].(string)
	forum.PicPath = forumMap["picpath"].(string)
	forum.Detail = forumMap["detail"].(string)
	forum.UnionID = forumMap["unionid"].(string)
	forum.VideoPath = forumMap["videopath"].(string)
	if forumservice.AddText(forum) {
		c.JSON(http.StatusOK, gin.H{
			"msg": http.StatusText(http.StatusOK),
		})
		return
	} else {
		c.JSON(http.StatusOK, gin.H{
			"msg":   "Fail",
			"ERROR": "Add forum text Fail!",
		})
		return
	}
}

// AddForumComment 添加评论
func AddForumComment(c *gin.Context) {
	// 用map读取json数据
	var commentMap = make(map[string]interface{})
	c.BindJSON(&commentMap)
	fmt.Println(commentMap)
	//读取评论信息，其中的forumid为帖子id
	commentDetail := commentMap["detail"].(string)
	commentUnionID := commentMap["unionid"].(string)
	commentIDStr := commentMap["commentid"].(string)
	if commentIDStr == "" {
		c.JSON(http.StatusOK, gin.H{
			"msg":   "Fail",
			"ERROR": "commentid is invaild!",
		})
		return
	}
	commentID, err := strconv.Atoi(commentIDStr)
	Tools.CherrError(err, "Add forum comment ERROR!")
	// 创建实体变量
	comment := domain.Forum{
		Detail:  commentDetail,
		UnionID: commentUnionID,
		ID:      commentID,
	}
	//添加评论
	if forumservice.AddComment(comment) {
		c.JSON(http.StatusOK, gin.H{
			"msg": http.StatusText(http.StatusOK),
		})
		return
	} else {
		c.JSON(http.StatusOK, gin.H{
			"msg":   "Fail",
			"ERROR": "Add forum comment Fail!",
		})
		return
	}
}

// GetForumText 查看帖子
func GetForumText(c *gin.Context) {
	//	读取页面的数据
	var textMap = make(map[string]interface{})
	c.BindJSON(&textMap)
	textIDStr := textMap["forumid"].(string)
	if textIDStr == "" || textIDStr == "-1" {
		c.JSON(http.StatusOK, gin.H{
			"msg":   "Fail",
			"ERROR": "forumid is invaild!",
		})
		return
	}
	textid, err := strconv.Atoi(textIDStr)
	Tools.CherrError(err, "Get forum text detail ERROR!")
	//	调用后端，返回帖子信息
	if isOk, forum := forumservice.GetTextDetail(textid); isOk {
		c.JSON(http.StatusOK, gin.H{
			"msg":  http.StatusText(http.StatusOK),
			"data": forum,
		})
		return
	} else {
		c.JSON(http.StatusOK, gin.H{
			"msg":   "Fail",
			"ERROR": "Get forum text Fail!",
		})
		return
	}
}

// GetForumTextList 帖子列表
func GetForumTextList(c *gin.Context) {
	//	读取页面的数据
	var textMap = make(map[string]interface{})
	c.BindJSON(&textMap)
	textIDStr := textMap["forumid"].(string)
	if textIDStr == "" || textIDStr == "-1" {
		c.JSON(http.StatusOK, gin.H{
			"msg":   "Fail",
			"ERROR": "forumid is invaild!",
		})
		return
	}
	textid, err := strconv.Atoi(textIDStr)
	Tools.CherrError(err, "Get forum text detail ERROR!")
	//	获取帖子列表
	if isOk, forums := forumservice.GetTextList(textid); isOk {
		c.JSON(http.StatusOK, gin.H{
			"msg":  http.StatusText(http.StatusOK),
			"data": forums,
		})
		return
	} else {
		c.JSON(http.StatusOK, gin.H{
			"msg":   "Fail",
			"ERROR": "Get forum text Fail!",
		})
		return
	}
}

// GetForumCommentList 评论列表
func GetForumCommentList(c *gin.Context) {
	//	读取页面的数据
	var commentMap = make(map[string]interface{})
	c.BindJSON(&commentMap)
	textIDStr := commentMap["forumid"].(string)
	if textIDStr == "" || textIDStr == "-1" {
		c.JSON(http.StatusOK, gin.H{
			"msg":   "Fail",
			"ERROR": "forumid is invaild!",
		})
		return
	}
	commentid, err := strconv.Atoi(textIDStr)
	Tools.CherrError(err, "Get forum text detail ERROR!")
	//	获取评论列表
	if isOk, comments := forumservice.GetCommentList(commentid); isOk {
		c.JSON(http.StatusOK, gin.H{
			"msg":  http.StatusText(http.StatusOK),
			"data": comments,
		})
		return
	} else {
		c.JSON(http.StatusOK, gin.H{
			"msg":   "Fail",
			"ERROR": "Get forum comment Fail!",
		})
		return
	}
}

// GetPicture 接受图片返回路径
func GetPicture(c *gin.Context) {
	//	获取图片并且存储下来
	pictureFile, err := c.FormFile("picpath")
	Tools.CherrError(err, "Get picture ERROR!")
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"msg":   "Fail",
			"ERROR": "Get upload source file Fail!",
		})
		return
	}
	filePath := Tools.PicturePath + "Forum/Text/" + pictureFile.Filename
	err = c.SaveUploadedFile(pictureFile, filePath)
	Tools.CherrError(err, "Save picture ERROR!")
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"msg":   "Fail",
			"ERROR": "Save source file Fail!",
		})
		return
	}
	// 返回图片存储信息
	c.JSON(http.StatusOK, gin.H{
		"msg":     http.StatusText(http.StatusOK),
		"picpath": "http://" + Tools.IPAddr + Tools.Port + "/Picture/Forum/Text/" + pictureFile.Filename,
	})
}
